//  肌肉率
import MeasureItem from './MeasureItem'
import Calc from './Calc'

const UI = require('./UI')
var desc = "您的肌肉率为 XX% ，处于YY状态，拥有更多的肌肉能让您展示更好体型。";
const STAND_LEVEL = 1;

export default class MuscleRateReport {
  /**
   * 肌肉率
   */
  static initValue(weight, skeletalMuscle) {

    return parseFloat(((skeletalMuscle / weight) * 100).toFixed(2))

  }

  static initValues(measure) {
    let values = 0;
    let weight = measure.weight;

    if (measure.user.gender == 'male') {

      if (measure.user.height < 160) {
        values = [MuscleRateReport.initValue(weight, 38.50), MuscleRateReport.initValue(weight, 46.50)]
      }
      else if (measure.user.height <= 170) {
        values = [MuscleRateReport.initValue(weight, 44.00), MuscleRateReport.initValue(weight, 52.40)]
      } else {
        values = [MuscleRateReport.initValue(weight, 49.40), MuscleRateReport.initValue(weight, 59.40)]
      }
    } else {
      if (measure.user.height < 150) {
        values = [MuscleRateReport.initValue(weight, 29.10), MuscleRateReport.initValue(weight, 34.70)]

      } else if (measure.user.height <= 160) {
        values = [MuscleRateReport.initValue(weight, 32.90), MuscleRateReport.initValue(weight, 37.50)]

      } else {
        values = [MuscleRateReport.initValue(weight, 36.50), MuscleRateReport.initValue(weight, 42.50)]

      }
    }

    return values;

  }

  static isEnable(measure) {
    return measure.bodyfat != 0
  }

  static build(measure) {

    const item = new MeasureItem();
    const values = MuscleRateReport.initValues(measure);
    const value = MuscleRateReport.initValue(measure.weight, measure.muscleMass);
    const boundaries = values;
    const level = Calc.calcLevel(value, boundaries, STAND_LEVEL);

    const isStand = level == STAND_LEVEL;

    item.key = 'muscleRate'
    item.name = "肌肉率";
    item.boundaries = boundaries;
    item.value = value;
    item.levelNames = ["偏低", "标准", "充足"];
    item.isStand = isStand;
    item.unit = "%";
    item.desc = desc.replace("XX", item.value).replace("YY", item.levelNames[level]);
    item.level = level;
    item.bar = [UI.color.report_lower, UI.color.report_stand, UI.color.report_higher];
    item.icon = 'report_bmi.png';

    item.min = 5;
    item.max = 65;

    if (!isStand) {
      let offset = 0;
      if (level < STAND_LEVEL) {
        offset = value - boundaries[STAND_LEVEL - 1]
      } else {
        offset = value - boundaries[STAND_LEVEL]
      }
      item.offset = offset
    }

    return item
  }

}
